package com.kyc.kycxkplaygame.activity;

import android.support.v7.widget.*;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.activity.base.BaseActivity;
import com.kyc.kycxkplaygame.adapter.ShoppingMoneyAdapter;
import com.kyc.kycxkplaygame.adapter.ShoppingPropAdapter;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.entity.CaomeiShoppingBean;
import com.kyc.kycxkplaygame.entity.PropTypeInfoBean;
import com.kyc.kycxkplaygame.view.GridSpacingItemDecoration;
import com.zhy.android.percent.support.PercentLinearLayout;
import es.dmoral.toasty.Toasty;

import java.util.ArrayList;
import java.util.List;

public class ShoppingActivity extends BaseActivity implements View.OnClickListener {

    private ImageView shopping_close;
    private TextView normal_prop;
    private TextView normal_huobi_prop;
    private RecyclerView shopping_recyclerView;
    private ShoppingPropAdapter shoppingPropAdapter;
    private TextView shopping_caomei_num;
    private PercentLinearLayout prop_layout;
    private PercentLinearLayout caomei_layout;
    private RecyclerView shopping_caomei_recyclerView;
    private ShoppingMoneyAdapter shoppingMoneyAdapter;
    private String state;

    @Override
    protected void initView() {
        state = getIntent().getStringExtra("state");

        shopping_close = findViewById(R.id.shopping_close);
        normal_prop = findViewById(R.id.normal_prop);
        normal_huobi_prop = findViewById(R.id.normal_huobi_prop);
        shopping_caomei_num = findViewById(R.id.shopping_caomei_num);
        prop_layout = findViewById(R.id.prop_layout);
        caomei_layout = findViewById(R.id.caomei_layout);

        shopping_recyclerView = findViewById(R.id.shopping_recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        shopping_recyclerView.setLayoutManager(gridLayoutManager);
        shopping_recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
        shopping_recyclerView.setItemAnimator(new DefaultItemAnimator());
        shoppingPropAdapter = new ShoppingPropAdapter(this);
        shopping_recyclerView.setAdapter(shoppingPropAdapter);

        shopping_caomei_recyclerView = findViewById(R.id.shopping_caomei_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        shopping_caomei_recyclerView.setLayoutManager(linearLayoutManager);
        shopping_recyclerView.setItemAnimator(new DefaultItemAnimator());
        shoppingMoneyAdapter = new ShoppingMoneyAdapter(this);
        shopping_caomei_recyclerView.setAdapter(shoppingMoneyAdapter);

        if(state.equals("normal")) {
            normal_prop.setSelected(true);
            normal_huobi_prop.setSelected(false);
            prop_layout.setVisibility(View.VISIBLE);
            caomei_layout.setVisibility(View.GONE);
        }else if(state.equals("caomei")){
            normal_prop.setSelected(false);
            normal_huobi_prop.setSelected(true);
            prop_layout.setVisibility(View.GONE);
            caomei_layout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        alasbusyIApi.getShoppingProp();
        String caomei = GetPropLevelUtils.getInstance().getPropNum("草莓");
        if (!TextUtils.isEmpty(caomei)) {
            shopping_caomei_num.setText(caomei);
        } else {
            shopping_caomei_num.setText("0");
        }
        List<CaomeiShoppingBean> list = new ArrayList<>();
        list.add(new CaomeiShoppingBean("1", 50, 10, 1.0));
        list.add(new CaomeiShoppingBean("2", 150, 50, 3.0));
        list.add(new CaomeiShoppingBean("3", 250, 80, 5.0));
        list.add(new CaomeiShoppingBean("4", 500, 100, 10.0));
        shoppingMoneyAdapter.setList(list);
        shoppingMoneyAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initListener() {
        shopping_close.setOnClickListener(this);
        normal_prop.setOnClickListener(this);
        normal_huobi_prop.setOnClickListener(this);

        shoppingPropAdapter.setOnItemClick(new ShoppingPropAdapter.OnItemclick() {
            @Override
            public void onclick(PropTypeInfoBean.PropTypeBean typeBean) {
                //购买,草莓币支付
                payBuyProp(typeBean);
            }
        });

        shoppingMoneyAdapter.setOnItemClick(new ShoppingMoneyAdapter.OnItemclick() {
            @Override
            public void onclick(double pricemoney) {
                //进入支付界面
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_shopping;
    }

    @Override
    protected void getSuccess(Object object) {
        if (object instanceof PropTypeInfoBean) {
            PropTypeInfoBean propTypeInfoBean = (PropTypeInfoBean) object;
            List<PropTypeInfoBean.PropTypeBean> propType = propTypeInfoBean.getPropType();
            List<PropTypeInfoBean.PropTypeBean> list = new ArrayList<>();
            if (propType.size() > 0) {
                for (PropTypeInfoBean.PropTypeBean bean : propType) {
                    if (!bean.getPropName().equals("体力") && !bean.getPropName().equals("葡萄") && !bean.getPropName().equals("草莓")) {
                        list.add(bean);
                    }
                }
            }
            shoppingPropAdapter.setList(list);
            shoppingPropAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.normal_huobi_prop:
                prop_layout.setVisibility(View.GONE);
                caomei_layout.setVisibility(View.VISIBLE);
                /*normal_huobi_prop.setBackgroundResource(R.drawable.tab_press);
                normal_prop.setBackgroundResource(R.drawable.tab_normal);*/
                normal_prop.setSelected(false);
                normal_huobi_prop.setSelected(true);

                break;
            case R.id.shopping_close:
                this.finish();
                break;
            case R.id.normal_prop:
                prop_layout.setVisibility(View.VISIBLE);
                caomei_layout.setVisibility(View.GONE);
               /* normal_huobi_prop.setBackgroundResource(R.drawable.tab_normal);
                normal_prop.setBackgroundResource(R.drawable.tab_press);*/
                normal_prop.setSelected(true);
                normal_huobi_prop.setSelected(false);
                break;
        }
    }


    private void payBuyProp(PropTypeInfoBean.PropTypeBean typeBean) {
        String caomeiNum = GetPropLevelUtils.getInstance().getPropNum("草莓");
        if (Integer.parseInt(caomeiNum) > typeBean.getPropPrice()) {
            int nowCaomeiNum = (int) (Integer.parseInt(caomeiNum) - typeBean.getPropPrice());
            shopping_caomei_num.setText(nowCaomeiNum+"");
            GetPropLevelUtils.getInstance().updateNoFriutPropNum(typeBean.getPropName(), 1);
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), typeBean.getPropName(), 1);
        }else {
            Toasty.warning(this,"草莓币不够,请先购买草莓币.", Toast.LENGTH_SHORT).show();
        }
    }
}
